package com.slkj.shilixiaoyuanapp.ui.common;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.tencent.smtt.sdk.TbsReaderView;

import butterknife.BindView;

@ActivityInfo(layout = R.layout.activity_browse_files)
public class BrowseFilesActivity extends BaseActivity implements TbsReaderView.ReaderCallback{

    @BindView(R.id.layout_root)
    FrameLayout root;
    TbsReaderView mTbsReaderView;
    @InjectParam String filePath;
    @InjectParam String fileName;
    @Override
    protected void init() {
        BrowseFilesActivity__JumpCenter.bind(this);
        mTbsReaderView = new TbsReaderView(this, this);

        setTitle(fileName);
        Log.d(TAG, ">>>init---" + filePath);
        displayFile();
    }

    @Override
    public void onCallBackAction(Integer integer, Object o, Object o1) {

    }
    private void displayFile() {
        Bundle bundle = new Bundle();
        bundle.putString(TbsReaderView.KEY_FILE_PATH, filePath);
        bundle.putString(TbsReaderView.KEY_TEMP_PATH, Environment.getExternalStorageDirectory().getPath()+"/temp");
        boolean b = mTbsReaderView.preOpen(parseFormat(filePath), true);//第一个参数代表问的的格式

        if (b) {
            mTbsReaderView.openFile(bundle);//打开文档
        }else {
            showToast("不支持此类文件");
        }
        root.addView(mTbsReaderView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
    }
    private String parseFormat(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTbsReaderView.onStop();
    }
}
